package com.wandian.weather.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wandian.weather.R;
import com.wandian.weather.bean.CalendarItemBean;
import com.wandian.weather.tools.WeatherUtil;

import java.util.List;


public class New30DaysCalendarAdapter extends BaseQuickAdapter<CalendarItemBean, BaseViewHolder> {

    private OnCalendarClickListener onCalendarClickListener;

    private int pageNum = 0;
    private boolean isNotify = false;

    public New30DaysCalendarAdapter(int layoutResId, @Nullable List<CalendarItemBean> data) {
        super(layoutResId, data);
    }
    public void initChoosPage(int pageNum){
        this.pageNum = pageNum;
    }

    public void setOnCalendarClickListener(OnCalendarClickListener onCalendarClickListener) {
        this.onCalendarClickListener = onCalendarClickListener;
    }

    private LinearLayout chooseItemll = null;


    @Override
    protected void convert(BaseViewHolder helper, final CalendarItemBean item) {
        try {
            TextView tv = helper.getView(R.id.tv_calendar);
            final LinearLayout ll = helper.getView(R.id.ll_item_calendar_root);
            ImageView img = helper.getView(R.id.ll_item_calendar_img);

            tv.setTextColor(item.isOld() ? Color.parseColor("#80FFFFFF") : Color.parseColor("#FFFFFF"));
            tv.setText(item.getDateString());
            if (item.isWeek())
                img.setVisibility(View.GONE);
            else if (item.isOld())
                img.setVisibility(View.INVISIBLE);
            else {
                img.setVisibility(View.VISIBLE);
                img.setImageResource(WeatherUtil.getWeatherStateImage(item.getImgNum(), false));
            }

            if (item.isToday()){
                if(isNotify){
                    ll.setBackground(null);
                }else {
                    chooseItemll = ll;
                    ll.setBackgroundResource(R.drawable.shape_calendar_item);
                }

            }
            else ll.setBackground(null);


            ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(item.isOld() || item.isWeek()) return;
                    if(chooseItemll !=null && ll != chooseItemll){
                        chooseItemll.setBackground(null);
                        ll.setBackgroundResource(R.drawable.shape_calendar_item);
                        chooseItemll =  ll;
                    }else {
                        ll.setBackgroundResource(R.drawable.shape_calendar_item);
                        chooseItemll =  ll;
                    }
                    if (onCalendarClickListener != null)
                        onCalendarClickListener.calendarClick(item,pageNum);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnCalendarClickListener {
        void calendarClick(CalendarItemBean item, int pageNum);
    }
    public void notifyDataUi(boolean notify){
        isNotify= notify;
        if(notify){
            notifyDataSetChanged();
        }

    }
}
